<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>编辑博客</title>
    <link
      rel="stylesheet"
      href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"
      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
      crossorigin="anonymous"
    />
    <link rel="stylesheet" href="./common.css" />
  </head>
  <body>
    <div class="container">
      <div class="row justify-content-md-center">
        <div class="col-lg-5 col-md-12">
          <h1 class="display-8">编辑博客</h1>
          <form class="blog-list">
            <div class="form-group">
              <input
                type="text"
                class="form-control"
                id="text-title"
              />
            </div>
            <div class="form-group">
              <textarea
                class="form-control"
                id="text-content"
                rows="5"
              ></textarea>
            </div>

            <button
              type="button"
              class="btn btn-primary btn-lg btn-block"
              id="btn-update"
            >
              保存
            </button>
          </form>
        </div>
      </div>
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script>
      // 发送 get 请求
      function get(url) {
        return $.get(url);
      }

      // 发送 post 请求
      function post(url, data = {}) {
        return $.ajax({
          type: "post",
          url,
          data: JSON.stringify(data),
          contentType: "application/json"
        });
      }

      // 获取 url 参数
      function getUrlParams() {
        let paramStr = location.href.split("?")[1] || "";
        paramStr = paramStr.split("#")[0];
        const result = {};
        paramStr.split("&").forEach(itemStr => {
          const arr = itemStr.split("=");
          const key = arr[0];
          const val = arr[1];
          result[key] = val;
        });
        return result;
      }

      // 获取 dom 元素
      const $textTitle = $("#text-title");
      const $textContent = $("#text-content");
      const $btnUpdate = $("#btn-update");

      // 获取博客内容
      const urlParams = getUrlParams();
      const url = "/api/blog/detail?id=" + urlParams.id;
      get(url).then(res => {
        if (res.errno !== 0) {
          alert(res.message);
          return;
        }

        // 显示数据
        const data = res.data || {};
        $textTitle.val(data.title);
        $textContent.val(data.content);
        $btnUpdate.attr("data-id", data.id);
      });

      // 提交修改内容
      $btnUpdate.click(function() {
        const $this = $(this);
        const id = $this.attr("data-id");
        const title = $textTitle.val();
        const content = $textContent.val();

        const url = "/api/blog/update?id=" + id;
        const data = {
          title,
          content
        };
        post(url, data).then(res => {
          if (res.errno !== 0) {
            alert(res.message);
            return;
          }
          alert("更新成功");
          location.href = "/admin.html";
        });
      });
    </script>
  </body>
</html>
